package com.tdkj.service.risevalue.impl;

import java.sql.Timestamp;
import java.util.List;

import javax.annotation.Resource;

import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Component;

import com.tdkj.dao.risevalue.IBeanFloatColorDao;
import com.tdkj.model.information.BeanBaseInfo;
import com.tdkj.model.risevalue.BeanFloatColorBuy;
import com.tdkj.model.risevalue.BeanFloatColorCustom;
import com.tdkj.model.risevalue.BeanFloatColorUseInfo;
import com.tdkj.model.user.BeanUser;
import com.tdkj.service.BaseService;
import com.tdkj.service.ServiceException;
import com.tdkj.service.financial.IBeanConsumeRecordService;
import com.tdkj.service.risevalue.IBeanFloatColorService;


@Component
public class BeanFloatColorServiceImpl extends BaseService implements
		IBeanFloatColorService {
	
	private IBeanConsumeRecordService consumeRecordService;
	
	@Resource
	public void setConsumeRecordService(
			IBeanConsumeRecordService consumeRecordService) {
		this.consumeRecordService = consumeRecordService;
	}

	private  IBeanFloatColorDao beanFloatColorDao;
    
	@Resource
	public void setBeanFloatColorDao(IBeanFloatColorDao beanFloatColorDao) {
		this.beanFloatColorDao = beanFloatColorDao;
	}

	@Override
	public void createBuy(Long useInfoId, Long customId, Integer color, Long userId) throws ServiceException {
		
		BeanUser user = beanFloatColorDao.findById(userId, BeanUser.class);
		BeanFloatColorCustom custom = beanFloatColorDao.findById(customId, BeanFloatColorCustom.class);
		BeanFloatColorUseInfo useInfo = beanFloatColorDao.findById(useInfoId, BeanFloatColorUseInfo.class);
		
		BeanFloatColorBuy bean = new BeanFloatColorBuy();
		bean.setUser(user);
		bean.setCustom(custom);
		bean.setColor(color);
		bean.setBuyTime(new Timestamp(System.currentTimeMillis()));
		bean.setUseInfo(useInfo);
		beanFloatColorDao.save(bean);
		
		useInfo.setIsUsing(true);
		
		consumeRecordService.create(userId, custom.getPrice(), custom.getUnit(), bean.getId(), BeanFloatColorBuy.class, "飘彩");
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<BeanFloatColorBuy> getByUserId(Long userId) {
		BeanUser user = beanFloatColorDao.findById(userId, BeanUser.class);
		return beanFloatColorDao.getSession().createCriteria(BeanFloatColorBuy.class).add(Restrictions.eq("user", user)).list();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<BeanBaseInfo> getInfos(Long userId, boolean isFloated) {
		BeanUser user = beanFloatColorDao.findById(userId, BeanUser.class);
		if(isFloated){
			return beanFloatColorDao.getSession().createCriteria(BeanBaseInfo.class)
			.add(Restrictions.eq("user", user))
			.add(Restrictions.isNotNull("floatColor"))
			.list();
		}else{
			return beanFloatColorDao.getSession().createCriteria(BeanBaseInfo.class)
			.add(Restrictions.eq("user", user))
			.add(Restrictions.isNull("floatColor"))
			.list();
		}
	}

}
